package com.xxj.puzzlesheep.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.xxj.puzzlesheep.core.LImage;

import android.graphics.BitmapFactory;

/**
 * 
 * Copyright 2008 - 2009
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @project loonframework
 * @author chenpeng
 * @email ceponline@yahoo.com.cn
 * @version 0.1.0
 */
public class LGraphicsUtils {

	final static private Map<String, LImage> cacheImages = new HashMap<String, LImage>(
			100);

	/**
	 * 加载Bitmap
	 * 
	 * @param innerFileName
	 * @return
	 */
	final static public LImage loadLAImage(final String innerFileName) {
		if (innerFileName == null) {
			return null;
		}
		String innerName = replaceIgnoreCase(innerFileName, "\\", "/");
		String keyName = innerName.toLowerCase();
		LImage image = cacheImages.get(keyName);
		if (image == null) {
			InputStream in = null;
			try {
				in = LGraphicsUtils.class.getResourceAsStream(innerFileName);
				image = new LImage(BitmapFactory.decodeStream(in));
				cacheImages.put(keyName, image);
			} catch (Exception e) {
				System.err.println(innerFileName + " not found!");
			} finally {
				try {
					if (in != null) {
						in.close();
						in = null;
					}
				} catch (IOException e) {
				}
			}
		}
		if (image == null) {
			System.err.println(("File not found. ( " + innerFileName + " )")
					.intern());
		}
		return image;
	}

	/**
	 * 不匹配大小写的过滤指定字符串
	 * 
	 * @param line
	 * @param oldString
	 * @param newString
	 * @return
	 */
	public static final String replaceIgnoreCase(String line, String oldString,
			String newString) {
		if (line == null)
			return null;
		String lcLine = line.toLowerCase();
		String lcOldString = oldString.toLowerCase();
		int i = 0;
		if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
			char line2[] = line.toCharArray();
			char newString2[] = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j;
			for (j = i; (i = lcLine.indexOf(lcOldString, i)) > 0; j = i) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
			}

			buf.append(line2, j, line2.length - j);
			return buf.toString();
		} else {
			return line;
		}
	}

	public static void destroyImages() {
		cacheImages.clear();
	}

}
